package com.ruoyi.basice_info.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.basice_info.domain.TsExamDetailsVo;
import com.ruoyi.basice_info.mapper.TsExamDetailsVoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basice_info.mapper.TbKnowledgePointsMapper;
import com.ruoyi.basice_info.domain.TbKnowledgePoints;
import com.ruoyi.basice_info.service.ITbKnowledgePointsService;

/**
 * 知识点设置Service业务层处理
 *
 * @author hongyi
 * @date 2023-12-25
 */
@Service
public class TbKnowledgePointsServiceImpl implements ITbKnowledgePointsService
{
    @Autowired
    private TbKnowledgePointsMapper tbKnowledgePointsMapper;


     @Autowired
     private TsExamDetailsVoMapper tsExamDetailsVoMapper;
    /**
     * 查询知识点设置
     *
     * @param knowledgePointsId 知识点设置主键
     * @return 知识点设置
     */
    @Override
    public TbKnowledgePoints selectTbKnowledgePointsByKnowledgePointsId(Long knowledgePointsId)
    {
        return tbKnowledgePointsMapper.selectTbKnowledgePointsByKnowledgePointsId(knowledgePointsId);
    }

    @Override
    public TbKnowledgePoints getTbKnowledgePointsByKnowledgePointsCode(String code) {
        return tbKnowledgePointsMapper.getTbKnowledgePointsByKnowledgePointsCode(code);
    }

    @Override
    public TbKnowledgePoints getTbKnowledgePointsByKnowledgePointsName(String name) {
        return tbKnowledgePointsMapper.getTbKnowledgePointsByKnowledgePointsName(name);
    }

    /**
     * 查询知识点设置列表
     *
     * @param tbKnowledgePoints 知识点设置
     * @return 知识点设置
     */
    @Override
    public List<TbKnowledgePoints> selectTbKnowledgePointsList(TbKnowledgePoints tbKnowledgePoints)
    {
        return tbKnowledgePointsMapper.selectTbKnowledgePointsList(tbKnowledgePoints);
    }

    /**
     * 新增知识点设置
     *
     * @param tbKnowledgePoints 知识点设置
     * @return 结果
     */
    @Override
    public int insertTbKnowledgePoints(TbKnowledgePoints tbKnowledgePoints)
    {
        return tbKnowledgePointsMapper.insertTbKnowledgePoints(tbKnowledgePoints);
    }

    /**
     * 修改知识点设置
     *
     * @param tbKnowledgePoints 知识点设置
     * @return 结果
     */
    @Override
    public int updateTbKnowledgePoints(TbKnowledgePoints tbKnowledgePoints)
    {
        return tbKnowledgePointsMapper.updateTbKnowledgePoints(tbKnowledgePoints);
    }

    /**
     * 批量删除知识点设置
     *
     * @param knowledgePointsIds 需要删除的知识点设置主键
     * @return 结果
     */
    @Override
    public int deleteTbKnowledgePointsByKnowledgePointsIds(Long[] knowledgePointsIds)
    {
        int num = -1;
        int nums = 0;
        List<Long> longList = Arrays.asList(knowledgePointsIds);
        for (Long item: longList) {
            TsExamDetailsVo detailsVo = new TsExamDetailsVo();
//            detailsVo.setExamDetailsId(item);
             detailsVo.setKnowledgePointsId(item);
            // 去子表中查询
            // 删除一条的
            List<TsExamDetailsVo> voList = tsExamDetailsVoMapper.selectTsExamDetailsList(detailsVo);
            if (voList.size()<=0) {
                 num = tbKnowledgePointsMapper.deleteTbKnowledgePointsByKnowledgePointsId(item);
            } else  {
                nums ++;
            }
        }
        // 删除多条的；
        if (longList.size() == 1) {
            if(nums > 0) {
                throw new IllegalArgumentException("该数据考试详情中使用，不能删除");
            } else {
                if(nums > 0) {
                    throw new IllegalArgumentException(nums + " 条数据删除失败，该数据考试详情中使用，不能删除");
                }
            }
        }

//        return tbKnowledgePointsMapper.deleteTbKnowledgePointsByKnowledgePointsIds(knowledgePointsIds);

        return num;
    }

    /**
     * 删除知识点设置信息
     *
     * @param knowledgePointsId 知识点设置主键
     * @return 结果
     */
    @Override
    public int deleteTbKnowledgePointsByKnowledgePointsId(Long knowledgePointsId)
    {
        return tbKnowledgePointsMapper.deleteTbKnowledgePointsByKnowledgePointsId(knowledgePointsId);
    }
}
